// twl_dialogs.cpp
/*
 * Steve Donovan, 2003
 * This is GPL'd software, and the usual disclaimers apply.
 * See LICENCE
 */
#include <windows.h>
#include <commdlg.h>
#include "twl.h"
#include "twl_dialogs.h"

static char s_file_buffer[MAX_PATH], s_initial_dir[MAX_PATH];

TOpenFile::TOpenFile(TEventWindow *parent,const char *caption, const char *filter, bool do_prompt)
{
 OPENFILENAME& ofn = *new OPENFILENAME;
 m_ofn = &ofn;
 m_prompt = do_prompt;
 m_filename = new char [MAX_PATH];
 *m_filename = '\0';
 char* p_filter = strdup(filter);
 for(char* p = p_filter; *p; p++)
     if (*p=='|') *p = '\0';

 ZeroMemory(m_ofn,sizeof(OPENFILENAME));
 ofn.lStructSize = sizeof(OPENFILENAME);
 ofn.hwndOwner = (HWND)parent->handle();
 ofn.lpstrFilter = p_filter;
 ofn.nFilterIndex = 1;
 ofn.nMaxFile = MAX_PATH;
 //ofn.nMaxFileTitle = 400;
 //ofn.lpstrDefExt = "txt"; 
 //ofn.lpstrFileTitle = (char*)caption;
 ofn.lpstrTitle = (char*)caption;
 ofn.lpstrFile = m_filename;

 GetCurrentDirectory(MAX_PATH,s_initial_dir);
 initial_dir(s_initial_dir);
}

TOpenFile::~TOpenFile()
{
 delete LPOPENFILENAME(m_ofn);
 delete m_filename;
}

void TOpenFile::initial_dir(const char *dir)
{
 LPOPENFILENAME(m_ofn)->lpstrInitialDir = dir;
}

bool TOpenFile::go()
{
 if (m_prompt) LPOPENFILENAME(m_ofn)->Flags = OFN_CREATEPROMPT;
 return GetOpenFileName((LPOPENFILENAME)m_ofn);
}

const char *TOpenFile::file_name()
{
 return m_filename;
}

void TOpenFile::file_name(const char *file)
{
 strcpy(m_filename,file);
}

bool TSaveFile::go()
{
 if (m_prompt) LPOPENFILENAME(m_ofn)->Flags = OFN_OVERWRITEPROMPT;
 return GetSaveFileName((LPOPENFILENAME)m_ofn);
}
